function [KNNList, SortedDistTable] = Ddavid_find_knn(K, AllData)

disp(['Ddavid_find_knn K = ' num2str(K)]);

N = size(AllData, 1);
KNNList = zeros(N, K);
SortedDistTable = zeros(N, K);

for i = 1:N
    AllDist = dist2(AllData(i, :), AllData);
    [DistTable, Index] = sort(AllDist);
    KNNList(i, :) = Index(1, 2:(K + 1));
    SortedDistTable(i, :) = DistTable(1, 2:(K + 1));
end
